package com.own.business.question.entity.model;

import com.own.business.question.common.base.ReplyEnvironment;
import com.own.business.question.common.em.NotFoundReplyEnvironment;
import com.own.business.question.common.em.QuestionType;
import com.own.component.common.util.function.ListFunction;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PracticeConfig
 *
 * @author chenxueli
 * @date 2021/5/17 21:00
 */
public class PracticeConfig {

    private final Map<QuestionType, Integer> MAP;
    @Getter
    private Integer total;
    /**
     * 作答环境
     */
    @Getter
    @Setter
    private ReplyEnvironment environment;

    /**
     * 获取题目列表的函数
     */
    private ListFunction<Long> questionIdList;

    /**
     * 缓存的题目id列表
     */
    private List<Long> storeQuestionIdList;

    /**
     * 是否需要答案
     */
    @Getter
    @Setter
    private boolean needAnswer;

    /**
     * 是否打乱
     */
    @Getter
    @Setter
    private boolean shuffle;

    public PracticeConfig() {
        this.MAP = new HashMap<>();
        this.total = 0;
        this.storeQuestionIdList = null;
        this.questionIdList = null;
        this.environment = NotFoundReplyEnvironment.NONULL;
        this.shuffle = true;
        this.needAnswer = false;
    }

    public PracticeConfig(ReplyEnvironment environment) {
        this();
        this.environment = environment;
    }

    public PracticeConfig(ReplyEnvironment environment, ListFunction<Long> listFunction) {
        this(environment);
        this.questionIdList = listFunction;
    }

    public PracticeConfig(ReplyEnvironment environment, List<Long> questionIdList) {
        this(environment);
        this.storeQuestionIdList = questionIdList;
    }

    @SuppressWarnings("all")
    public PracticeConfig putType(QuestionType type, Integer count) {
        total -= MAP.getOrDefault(type, 0);
        MAP.put(type, count);
        total += count;
        return this;
    }

    public Map<QuestionType, Integer> types() {
        return MAP;
    }

    /**
     * 获取题目列表
     *
     * @return 题目id列表
     */
    public List<Long> getQuestionIdList() {
        if (storeQuestionIdList == null) {
            storeQuestionIdList = questionIdList == null ? new ArrayList<>() : questionIdList.list();
        }
        return storeQuestionIdList;
    }

    /**
     * 设置题目列表
     *
     * @param listFunction 题目列表方法
     * @return 当前对象
     */
    public PracticeConfig setQuestionIdList(ListFunction<Long> listFunction) {
        this.storeQuestionIdList = null;
        this.questionIdList = listFunction;
        return this;
    }

    /**
     * 设置题目列表
     *
     * @param questionIdList 题目列表
     * @return 当前对象
     */
    public PracticeConfig setQuestionIdList(List<Long> questionIdList) {
        this.storeQuestionIdList = questionIdList;
        return this;
    }

}
